package com.touch.account;
import java.io.File;
import java.io.IOException;

import com.touch.core.AccountConstants;
import com.touch.core.Constants;
import com.touch.core.avatar.Avatar;
import com.touch.core.script.ByteArray;
import com.touch.core.script.ByteDecoder;
import com.touch.core.util.IOTools;
public class Account extends ByteDecoder
{
	public String name;
	public String password;
	public boolean banned;
	public Avatar activeCharacter;	
	public byte gender;
	
	private Account(){}
	private Account(String name, String password)
	{		
		this.banned = false;
		this.name = name;
		this.password = password;	
		this.save();
	}
	public static void CHECKACCOUNTS()
	{
		File accdir = new File("Accounts");
		if(accdir.exists())
			return;
		else
			accdir.mkdir();
	}
	public static Account loadAccount(String name)
	{
		File accdat = new File(getDataPath(name));
		if(!accdat.exists())
			return null;
	
		final Account account = new Account();
		ByteArray data = new ByteArray(IOTools.readFileAsBytes(getDataPath(name)));
		account.name = name;
		account.decode(data);
		return account;
	}
	private static String getDataPath(String name)
	{
		return AccountConstants.ACCOUNT_DIR + name + "/data.dat";
	}	
	public static Account makeAccount(String accountName, String password) throws IOException
	{
		File accDir = new File(AccountConstants.ACCOUNT_DIR + accountName);
				
		accDir.mkdir();		
		return new Account(accountName, password);
		
	}
	public static boolean isValidNewAccount(String accountName)
	{
		File accDir = new File(AccountConstants.ACCOUNT_DIR + accountName);
		if(accDir.exists())
			return false;
		return true;
	}			
	public void save()
	{
		ByteArray data = new ByteArray();
		encodeBanned(data);
		encodePassword(data);
		IOTools.writeBytesToFile(getDataPath(name), data.toArray());
	}	
	public void encodePassword(ByteArray data)
	{
		data.appendTo(Constants.com_START);
		data.appendTo(AccountCode.PASSWORD.getValue());
		data.appendTo(password);
		data.appendTo(Constants.com_END);
	}
	public void encodeBanned(ByteArray data)
	{
		data.appendTo(Constants.com_START);
		data.appendTo(AccountCode.BANNED.getValue());
		if(banned)
			data.appendTo((byte)1);
		else
			data.appendTo((byte)0);
		data.appendTo(Constants.com_END);
	}	
	public void encodeGender(ByteArray data)
	{
		data.appendTo(Constants.com_START);
		data.appendTo(AccountCode.GENDER.getValue());
		data.appendTo(this.gender);
		data.appendTo(Constants.com_END);
	}
	@Override
	public void parser(ByteArray command)
	{
		AccountCode accCode = AccountCode.getAccountCode(command.toArray()[0]);
		command = command.subArray(1, command.length());
		
		if(accCode == AccountCode.BANNED)
			parseBanned(command);
		else if(accCode == AccountCode.PASSWORD)
			parsePassword(command);
		else if(accCode == AccountCode.GENDER)
			parseGender(command);
	}
	private void parseBanned(ByteArray command)
	{		
		if(command.toByte() == 0)
			this.banned = false;
		else
			this.banned = true;
	}
	private void parsePassword(ByteArray command)
	{		
		this.password = command.toString();		
	}	
	private void parseGender(ByteArray command)
	{
		this.gender = command.toByte();
	}
}
